package cn.neusoft.controller;

import cn.neusoft.pojo.Address;
import cn.neusoft.query.AddressQuery;
import cn.neusoft.service.IAddressService;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    IAddressService addressService;

    @PostMapping      //  请求方式 + 请求路径   post  /address
    public ResultBean add(@RequestBody Address address){
        System.out.println("----AddressController-----add--------");
        System.out.println(address);
        return addressService.add(address);
    }



    @PutMapping    //   put   /address
    public ResultBean update(@RequestBody Address address){
        System.out.println("-------AddressController--------update-------------");
        System.out.println(address);
        return addressService.update(address);
    }

    //  delete   /address/1003
    @DeleteMapping("/{id}")   // http://localhost:8080/address/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------AddressController--------del-------------");
        System.out.println(id);
        return addressService.delete(id);
    }

    //  get   /address/1004
    @GetMapping("/{id}")   // http://localhost:8080/address/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------AddressController--------getById-------------");
        System.out.println(id);
        return addressService.getById(id);
    }

    @GetMapping("/all")    //  get  /address/all
    public ResultBean getAll(){
        System.out.println("-------AddressController-----getAll--------");
        return addressService.getAll();
    }

    @PostMapping("/page")   // post  /address/page
    public ResultBean getByPage(@RequestBody AddressQuery addressQuery){
        System.out.println("-------AddressController--------getByPage-------------");
        System.out.println(addressQuery);
        return addressService.getByPage(addressQuery);
    }

    @GetMapping("/user/{userId}")    //   get +  /address/user/1
    public ResultBean getByUserId(@PathVariable("userId") Integer userId){
        System.out.println("--------AddressController--------getByUserId---------");
        System.out.println(userId);
        return addressService.getByUserId(userId);
    }

}
